<template>
  <div class="app-container">
    <Form
      v-if="showSearch"
      v-model:formData="searchInfo"
      :formItems="formItems"
    >
      <template #footer>
        <div style="text-align: left">
          <el-button size="small" icon="Search" type="primary" @click="goSearch"
            >搜索</el-button
          >
          <el-button size="small" icon="Refresh" @click="resetSearch"
            >重置</el-button
          >
        </div>
      </template>
    </Form>
    <h3></h3>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"> </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="resetSearch"
      ></right-toolbar>
    </el-row>
    <h3>
      <el-button
        size="small"
        icon="Plus"
        type="primary"
        @click="add"
        v-hasPermi="['member:czCate:add']"
        >添加</el-button
      >
    </h3>
    <x-table
      :isLoading="isLoading"
      :propList="propList"
      :tableData="info.items"
    >
      <template #is_user="{ row }">
        <span v-if="row.user_id"> 是 </span>
        <span v-else>否</span>
      </template>
      <template #op="{ row }">
        <el-button
          type="primary"
          link
          icon="Edit"
          @click="change(row)"
          v-hasPermi="['member:czCate:change']"
          >编辑</el-button
        >
        <el-button
          type="primary"
          link
          icon="Delete"
          @click="del(row)"
          v-hasPermi="['member:czCate:del']"
          >删除</el-button
        >
      </template>
    </x-table>
    <pagination
      v-show="info.total > 0"
      :total="info.total"
      v-model:page="searchInfo.pageNum"
      v-model:limit="searchInfo.pageSize"
      @pagination="getInfo"
    />
    <Dialog
      width="30%"
      :title="dialogConfig.title"
      v-model:isShowDialog="isShowDialog"
      @confirm="submit"
    >
      <!-- <div>
        <p>{{ curItem.content }}</p>
      </div> -->
      <Form ref="formRef" v-bind="dialogConfig.formConfig"> </Form>
    </Dialog>
  </div>
</template>

<script>
import { propList, formItems, dialogConfig } from "./config";
import { getList, add, change, del } from "@/api/member/productCate";
export default {
  name: "merchantproductCateindex",
  data() {
    return {
      isLoading: false,
      curItem: {},
      dialogConfig,
      showSearch: true,
      isShowDialog: false,
      propList,
      formItems,
      info: {},
      searchInfo: {
        pageNum: 1,
        pageSize: 10,
        created_time: [],
      },
    };
  },
  computed: {},
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init() {
      this.getInfo();
    },
    async getInfo() {
      this.isLoading = true;
      const params = { ...this.searchInfo };
      const { data } = await getList(params);
      this.info = data;
      this.isLoading = false;
    },
    resetSearch() {
      this.searchInfo = this.$options.data.call(this).searchInfo;
      this.searchInfo.created_time = [];
      this.getInfo();
    },
    goSearch() {
      // 重置时间
      this.getInfo();
    },
    add() {
      const whiteList = ["sort", "desc"];
      // 处理表单部分数据不可编辑
      this.dialogConfig.formConfig.formItems.forEach((item) => {
        if (!whiteList.includes(item.prop)) {
          item.disabled = false;
        }
      });
      this.dialogConfig.formConfig.formData = {};
      this.isShowDialog = true;
    },
    change(item) {
      // 优先获取基于系统修改的分类
      if (item.cate) {
        item.desc = item.cate.desc;
        item.sort = item.cate.sort;
      }
      // 修改
      this.dialogConfig.formConfig.formData = { ...item };
      const whiteList = ["sort", "desc", "name"];
      // 处理表单部分数据不可编辑
      this.dialogConfig.formConfig.formItems.forEach((item) => {
        if (whiteList.includes(item.prop)) {
          item.disabled = false;
        } else {
          item.disabled = true;
        }
      });
      this.isShowDialog = true;
    },
    del(item) {
      console.log(item);
      // if (item.cate) {
      //   item = item.cate;
      // }
      this.$modal
        .confirm("是否确认删除吗？")
        .then(function () {
          return del(item.id);
        })
        .then(() => {
          this.getInfo();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          console.log(this.dialogConfig.formConfig.formData);
          this.dialogLoading = true;
          const params = this.dialogConfig.formConfig.formData;
          try {
            if (params.id) {
              await change(params);
            } else {
              await add(params);
            }
            this.$message.success("操作成功");
            this.getInfo();
            this.isShowDialog = false;
          } finally {
            this.dialogLoading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
